const util=require("../../utils/util.js");
const app=getApp();
Page({
  onLoad: function (options) {
    var _this=this;
    wx.request({
      url: app.globalData.url +'productList',
      method:'POST',
      data:{
        type:0,
        proId:0,
        index:app.globalData.indexFlag
      },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
      if(res.data.code==0){
         wx.showToast({
           title: res.data.msg,
           icon: 'none',
           duration: 2000
         })
       }else if(res.data.code==1){
         _this.setData({
          datalist:res.data.data
         })
       }
      }
    })
    // Do some initialize when page load.
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.setData({
        carts: app.globalData.carts
      });
  },
  data: {
    inputShowed: false,
    hiddenmodal:true,
    proType:"", //默认规格
    num:0,      //默认数量
    currentPro: '',  //默认选中产品
    carts:[], //购物车
    actionSheetHidden:true,
    focusId:0,  //选中产品编号
    inputVal:"", //搜索框内容
    type:0,  //获取列表途径 0：产品分类；1：搜索框
    emptyImg:'../../style/img/Bitmap.png',
    products: [{
      'name': '常规PCR',
      'img': '../../style/img/img0.png'
    }, {
      'name': '逆转录PCR',
      'img': '../../style/img/img1.png'
    }, {
      'name': '荧光定量PCR',
      'img': '../../style/img/img2.png'
    }, {
      'name': '克隆',
      'img': '../../style/img/img3.png'
    }, {
      'name': '一抗',
      'img': '../../style/img/img4.png'
    }, {
      'name': '二抗',
      'img': '../../style/img/img5.png'
    }, {
      'name': '标签内参',
      'img': '../../style/img/img6.png'
    }, {
      'name': '全部产品',
      'img': '../../style/img/img7.png'
    }]
  },
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
     inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: "",
      focusId: 0,
      datalist: [],
      type: 0
    });
    app.globalData.indexFlag = 0;
    this.getlist();
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  searchItem:function(e){
    this.setData({
      focusId: 7,
      datalist: [],
        type:1
    });
    app.globalData.indexFlag = 0;
    this.getlist();
  },
  oepnType:function(e){
    var key=e.currentTarget.dataset.type;
    this.setData({
      hiddenmodal:false,
      currentPro:key
    });
  },
  getType:function(e){
    this.setData({
      proType: e.currentTarget.dataset.ml
    });
  },
  cancel:function(e){
    this.setData({
      hiddenmodal: true,
      proType:"",
      num:0
    });
  },
  confirm:function(e){
    var key = this.data.currentPro.code+"||"+this.data.proType;
    var index =app.globalData.cartFlag.indexOf(key);
    if (this.data.num>0) {
      if(this.data.currentPro.proId!=3&&this.data.proType==""){
        wx.showToast({
          title:"请选择规格",
          icon: 'none',
          duration: 2000
        });
        return;
      }
      if(index==-1){
          app.globalData.cartFlag.push(key);
          var tempobj={
            "name":this.data.currentPro.name,
            "proType":this.data.proType,
            "num":this.data.num,
            "proId":this.data.currentPro.proId
          };
          app.globalData.carts.push(tempobj);
      }else{
        app.globalData.carts[index].num = this.data.num;
      }
    }else{
      if(index!=-1){
        app.globalData.carts.split(index,1);
        app.globalData.cartFlag.split(index,1);
      }
    }
  this.setData({
      hiddenmodal: true,
      proType: "",
      num: 0,
      carts: app.globalData.carts

    });
  },
  reduceNum:function(){
    if(this.data.num>0){
      this.setData({
        "num":this.data.num-1
      })
    }
  },
  addNum:function(){
    this.setData({
      "num":this.data.num+1
    })
  },
  showSheet:function(){
    this.setData({
      actionSheetHidden:false
    })
  },
  actionSheetChange: function (e) {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  addList:function(e){
    util.cartNum(0, e.currentTarget.dataset.index);
  },
  reduceList: function (e) {
    util.cartNum(1, e.currentTarget.dataset.index);
  },
  clearCart:function(){
    app.globalData.carts=[],
      app.globalData.cartFlag=[],
    this.setData({
      actionSheetHidden:true,
      carts: app.globalData.carts
    })
  },
  gotoOrder:function(){
    wx.navigateTo({
      url: '../order/order?&flag=0'
    })
  },
  searchData:function(e){
    this.setData({
      focusId: e.currentTarget.dataset.id,
      datalist:[],
      type:0
    });
    app.globalData.indexFlag=0;
    this.getlist();
  },
  getlist:function(e){
    var _this=this;
    if(e&&e.target.id=="more"){
      app.globalData.indexFlag++;
    }
    if(this.data.type==0){
      var parm={
        type: _this.data.type,
        proId: _this.data.focusId,
        index: app.globalData.indexFlag
      };
    }else if(this.data.type==1){
      var parm={
        type: _this.data.type,
        searchName:_this.data.inputVal,
        index:app.globalData.indexFlag
      };
    }
    wx.request({
      url: app.globalData.url + 'productList',
      method: 'POST',
      data: parm,
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        if (res.data.code == 1) {
          if(res.data.data.length==0){
            wx.showToast({
              title: "没有更多数据",
              icon: 'none',
              duration: 2000
            })
            return;
          }else{
            var newlist = _this.data.datalist.concat(res.data.data);
            _this.setData({
              datalist: newlist
            })
          }
         
        } else {
        
        }
      }
    })
  }
});